﻿using System;
using System.Threading.Tasks;
using XCrawler;
using XCrawler.HtmlParser;

namespace XCrawler.Example
{
    public class Program
    {
        static async Task Main(string[] args)
        {
            Console.WriteLine("请输入要获取的网页地址?");
            var CrawlerUrlStr = Console.ReadLine();

            var CrawlerUrls = CrawlerUrlStr.SplitBySignWithoutEmpty(",");


            var crawler = new HttpCrawler()
                .SetParsers(new HtmlWorkerParser())
                .OnStart((s, e) =>
                {
                    Console.WriteLine($"{Environment.NewLine}开始获取网页: {e.Uri.ToString()}");
                })
                .OnCompleted((s, e) =>
                {
                    var pb = e.ParserBag;
                    Console.WriteLine("===============================================");
                    Console.WriteLine($"爬虫抓取任务完成！合计 " + pb.SourceText.Length + " 个字符。");
                    Console.WriteLine($"标题：{pb.GetValue("HtmlTitle", "")}");
                    Console.WriteLine($"耗时：{pb.DownloadTime}毫秒");
                    Console.WriteLine($"线程：{pb.ThreadId}");
                    Console.WriteLine($"地址：{pb.CrawlerUrl.ToString()}");
                    Console.WriteLine($"状态：{(int)pb.StatusCode} - {pb.StatusCode.ToString()}");
                })
                .OnError((s, e) =>
                {
                    Console.WriteLine($"{Environment.NewLine}获取网页出错: {e.Uri.ToString()}");
                    Console.WriteLine($"{Environment.NewLine}错误原因: {e.Exception.Message}");
                });


            Parallel.For(0, CrawlerUrls.Length, async (i) =>
            {
                await crawler.SetUrl(CrawlerUrls[i]).StartAsync().ConfigureAwait(true);
            });


            //await name.CreateCrawler(new HtmlWorkerParser())
            //    .OnStart((s, e) =>
            //    {
            //        Console.WriteLine($"{Environment.NewLine}开始获取网页: {e.Uri.ToString()}");
            //    })
            //    .OnCompleted((s, e) =>
            //    {
            //        var pb = e.ParserBag;
            //        Console.WriteLine("===============================================");
            //        Console.WriteLine($"爬虫抓取任务完成！合计 " + pb.SourceText.Length + " 个字符。");
            //        Console.WriteLine($"标题：{pb.GetValue("HtmlTitle","")}");
            //        Console.WriteLine($"耗时：{ pb.DownloadTime }毫秒");
            //        Console.WriteLine($"线程：{ pb.ThreadId}");
            //        Console.WriteLine($"地址：{ pb.CrawlerUrl.ToString()}");
            //        Console.WriteLine($"状态：{ pb.StatusCode.ToString()}");
            //    })
            //    .OnError((s, e) =>
            //    {
            //        Console.WriteLine($"{Environment.NewLine}获取网页出错: {e.Uri.ToString()}");
            //        Console.WriteLine($"{Environment.NewLine}错误原因: {e.Exception.Message}");
            //    }).ToWorkerAsync().ConfigureAwait(false);



            Console.Write($"{Environment.NewLine}Press any key to exit...");
            Console.ReadKey(true);
        }
    }
}
